#include "qimage.h"

QNamespaceStart(Qui)

void QPutimage(int x, int y, QImage* image_beput,
		int transparency, IMAGE* target)
{
	IMAGE temp = image_beput->get_image();

	HDC target_dc = GetImageHDC(target);
	HDC image_dc = GetImageHDC(&temp);

	int width = image_beput->get_width();
	int height = image_beput->get_height();

	BLENDFUNCTION blend_function = { AC_SRC_OVER, 0, static_cast<BYTE>(transparency), AC_SRC_ALPHA };
	AlphaBlend(target_dc, x, y, width, height,
		image_dc, 0, 0, width, height,
		blend_function);
}

void QImage::cut_image(QImageCutReact cut_react, int width, int height)
{
	using namespace Gdiplus;

	IMAGE new_image = IMAGE(width, height);

	HDC memory_dc = CreateCompatibleDC(QGetImageHDC(&save_image));
	HBITMAP bit_map = CreateCompatibleBitmap(QGetImageHDC(&save_image), save_image.getwidth(), save_image.getheight());
	HBITMAP old_bit_map = (HBITMAP)SelectObject(memory_dc, bit_map);

	BitBlt(memory_dc, 0, 0, width, height, QGetImageHDC(&save_image), 0, 0, SRCCOPY);

	bit_map = (HBITMAP)SelectObject(memory_dc, old_bit_map);

	DeleteDC(memory_dc);

	Bitmap convert_image(bit_map, NULL);
	Graphics     draw_graphics(GetImageHDC(&new_image));
	GraphicsPath draw_path;

	TextureBrush brush(&convert_image);

	switch (cut_react)
	{
	case ImageCutEllipse:
	{
		QShapeSetHighQuality(draw_graphics);

		draw_graphics.FillEllipse(&brush, Rect{ 0, 0, width, height });

		save_image = new_image;

		break;
	}
	case ImageCutRectangle:
	{
		QShapeSetHighQuality(draw_graphics);

		draw_graphics.FillRectangle(&brush, Rect{ 0, 0, width, height });

		save_image = new_image;

		break;
	}
	}
}

void QImage::cut_image_with_shadow(QImageCutReact cut_react, int width,
	int height, int shadow_pixel)
{
	using namespace Gdiplus;

	IMAGE temp_image = save_image;
	save_image = IMAGE(width + shadow_pixel, height + shadow_pixel);

	QPutimage(0, 0, &temp_image, 255, &save_image);

	Graphics graphics(QGetImageHDC(&save_image));
	GraphicsPath draw_path;

	draw_path.AddRectangle(Rect{ 0, 0, width, height});

	// QDrawShadow(graphics, draw_path, shadow_pixel);

	graphics.SetPageUnit(UnitPixel);

	GraphicsPath& shadow_path = *(draw_path.Clone());

	Matrix shadow_matrix;
	shadow_matrix.Translate(shadow_pixel, shadow_pixel);

	shadow_path.Transform(&shadow_matrix);

	Region path_region(&draw_path);
	Region shaow_region(&shadow_path);

	shaow_region.Exclude(&path_region);

	PathGradientBrush gradient_brush(&shadow_path);
	gradient_brush.SetCenterColor(Color(32, 32, 32, 32));

	Color colors[] = {Color(0, 0, 0, 0)};

	INT   count = 1;

	gradient_brush.SetSurroundColors(colors, &count);
	gradient_brush.SetFocusScales(0.75f, 0.75f);

	graphics.FillRegion(&gradient_brush, &shaow_region);
}

QNamespaceEnd